package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ita.ios.jms.protocol.JmsProtocol;

import po.Client;
import po.Dish;
import po.Merchant;
import po.OrderItems;
import po.Orders;
import sender.QueueSender;
import service.OrdersService;
import util.Po2Vo;
import vo.OrdersVo;

@Controller
public class ComplainController {
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private QueueSender queueSender;
	
	@Autowired
	private Destination queueComplain;
	
	@RequestMapping(value = "/complain", method = {RequestMethod.POST})
	public String complain(HttpSession session, String oid, String complaints) {
		System.out.println(complaints);
		Orders orders = ordersService.findOrder(oid);
		String message;
		try {
			orders.setComplain(complaints);
			String rString = ordersService.updateOrdersGrade(orders);
			message = JmsProtocol.serializeOrderComplaint(orders.getoId(), orders.getMerchant().getmId(), orders.getClient().getCid());
			System.out.println(message);
			queueSender.send(queueComplain, message);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return "redirect:/waitingForReceiving";
	}
	
	@RequestMapping(value = "/complainFromHisoty", method = {RequestMethod.POST})
	@ResponseBody
	public OrdersVo complainFromHisoty(HttpSession session, String oid, String complaints) {
		System.out.println(complaints);
		Orders orders = ordersService.findOrder(oid);
		
		String message;
		try {
			orders.setComplain(complaints);
			String rString = ordersService.updateOrdersGrade(orders);
			message = JmsProtocol.serializeOrderComplaint(orders.getoId(), orders.getMerchant().getmId(), orders.getClient().getCid());
			System.out.println(message);
			queueSender.send(queueComplain, message);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		OrdersVo ordersVo = ordersService.findOrderVo(oid);
		return ordersVo;
	}
}
